package aplications.android;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

    /**
     * Adapter for our image files.
     */
    class ImageAdapter extends BaseAdapter {

        private Context _context;
        private Cursor _cursor;
        private int _columnIndex;
        
        public ImageAdapter(Context localContext, Cursor cursor, int columnIndex) {
            _context = localContext;
            _cursor = cursor;
            _columnIndex = columnIndex;
        }

        public int getCount() {
            return _cursor.getCount();
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ImageView picturesView;
            if (convertView == null) 
            {
                picturesView = new ImageView(_context);
                _cursor.moveToPosition(position);	// Move cursor to current position
                
                int imageID = _cursor.getInt(_columnIndex);	// Get the current value for the requested column
              
                // Set the content of the image based on the provided URI
                picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
                picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                picturesView.setPadding(8, 8, 8, 8);
                picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
            }
            else 
                picturesView = (ImageView)convertView;
            
            return picturesView;
        }
    }